/*******************************************************************************
 *	                           bsp cpu.c
 *******************************************************************************/
#include "bsp.h"
#include "bsp_adc_tp.h"

/*******************************************************************************
 * @brief  x- -> AIN0		->ONMSK = 02
 * @brief  y- -> AIN1		->ONMSK = 08
 * @brief  x+ -> AIN2		->ONMSK = 20
 * @brief  y+ -> AIN3		->ONMSK = 80
 * @brief  1=PP, 3=AIN
 *******************************************************************************/
// x+,x-加电压，测量y-
#define IO_MODE_YM()         \
	GPIOA->MODER |= 0xFFUL;  \
	GPIOA->MODER &= ~0x22UL; \
	GPIOA->BSRR = 0x000F0004

// y+,y-加电压，测量x-
#define IO_MODE_XM()         \
	GPIOA->MODER |= 0xFFUL;  \
	GPIOA->MODER &= ~0x88UL; \
	GPIOA->BSRR = 0x000F0008

// y+,x-加电压，测量x+
#define IO_MODE_Z1M()        \
	GPIOA->MODER |= 0xFFUL;  \
	GPIOA->MODER &= ~0x82UL; \
	GPIOA->BSRR = 0x000F0008

// y+,x+加电压，测量x-
#define IO_MODE_Z2M()        \
	GPIOA->MODER |= 0xFFUL;  \
	GPIOA->MODER &= ~0xA0UL; \
	GPIOA->BSRR = 0x000F0008

/*******************************************************************************
 * @brief  function
 *******************************************************************************/
void bsp_tp_tick_handle(void);
void bsp_tp_tick_init(void);

/*******************************************************************************
 * @brief  local var
 *******************************************************************************/
tp_adc_type tp;

/*******************************************************************************
 * @brief  bsp_tp_adc_init
 * \param[in] none
 * \retval: none
 *******************************************************************************/
void bsp_tp_adc_init(void)
{
	uint32_t reg;

	ADC1->CR2 &= ~ADC_CR2_ADON;

	// 打开扫描模式
	ADC1->CR1 |= ADC_CR1_SCAN;

	// 禁用连续模式
	ADC1->CR2 &= ~ADC_CR2_CONT;

	// 采样时间
	ADC1->SMPR1 = 0;
	reg = 7UL << ADC_SMPR2_SMP0_Pos;
	reg |= 7UL << ADC_SMPR2_SMP1_Pos;
	reg |= 7UL << ADC_SMPR2_SMP2_Pos;
	reg |= 7UL << ADC_SMPR2_SMP3_Pos;
	ADC1->SMPR2 = reg;

	// 注入组配置
	tp.xn_sel = 3UL << ADC_JSQR_JL_Pos;
	tp.xn_sel |= 0UL << ADC_JSQR_JSQ1_Pos;
	tp.xn_sel |= 0UL << ADC_JSQR_JSQ2_Pos;
	tp.xn_sel |= 0UL << ADC_JSQR_JSQ3_Pos;
	tp.xn_sel |= 0UL << ADC_JSQR_JSQ4_Pos;

	tp.yn_sel = 3UL << ADC_JSQR_JL_Pos;
	tp.yn_sel |= 1UL << ADC_JSQR_JSQ1_Pos;
	tp.yn_sel |= 1UL << ADC_JSQR_JSQ2_Pos;
	tp.yn_sel |= 1UL << ADC_JSQR_JSQ3_Pos;
	tp.yn_sel |= 1UL << ADC_JSQR_JSQ4_Pos;

	tp.z1_sel = 3UL << ADC_JSQR_JL_Pos;
	tp.z1_sel |= 2UL << ADC_JSQR_JSQ1_Pos;
	tp.z1_sel |= 2UL << ADC_JSQR_JSQ2_Pos;
	tp.z1_sel |= 2UL << ADC_JSQR_JSQ3_Pos;
	tp.z1_sel |= 2UL << ADC_JSQR_JSQ4_Pos;

	tp.z2_sel = tp.xn_sel;

	ADC1->JSQR = tp.xn_sel;

	// 注入组数据偏移
	ADC1->JOFR1 = 0;
	ADC1->JOFR2 = 0;
	ADC1->JOFR3 = 0;
	ADC1->JOFR4 = 0;

	// 使能ADC
	ADC1->CR2 |= ADC_CR2_ADON;

	// 触发注入组
	ADC1->CR2 |= ADC_CR2_JSWSTART;

	bsp_tp_tick_init();

	// app_timer_task_add(bsp_tp_tick_handle);
}

/*******************************************************************************
 * @brief  bsp_tp_adc_init
 * \param[in] none
 * \retval: none
 *******************************************************************************/
#define ATIM TIM6
#define ATIM_IRQN TIM6_DAC_IRQn
void bsp_tp_tick_init(void)
{
	uint32_t clk;

	__HAL_RCC_TIM6_CLK_ENABLE();
	__HAL_RCC_TIM6_FORCE_RESET();
	__HAL_RCC_TIM6_RELEASE_RESET();

	ATIM->CR1 = TIM_CR1_URS;
	ATIM->CR2 = 0;
	ATIM->SR = 0;
	ATIM->DIER = TIM_DIER_UIE;
	clk = HAL_RCC_GetPCLK1Freq();

	/*1ms interrupt*/
	ATIM->PSC = (clk / 1000000) * 2 - 1;
	ATIM->ARR = 500 - 1;
	ATIM->EGR = TIM_EGR_UG;

	nvic_irq_set(ATIM_IRQN, 0x01, ENABLE);

	ATIM->CR1 = TIM_CR1_CEN;
}

void TIM6_DAC_IRQHandler(void)
{
	ATIM->SR = 0;
	bsp_tp_tick_handle();
}

/*******************************************************************************
 * @brief  bsp_tp_adc_init
 * \param[in] none
 * \retval: none
 *******************************************************************************/
void bsp_tp_tick_handle(void)
{
	uint16_t adcv;

	if (ADC1->SR & ADC_SR_JEOC)
	{
		ADC1->SR = 0;

		// 获取AD值
		adcv = ADC1->JDR1;
		adcv += ADC1->JDR2;
		adcv += ADC1->JDR3;
		adcv += ADC1->JDR4;

		// 切换下一通道
		switch (tp.step)
		{
		case 0:
			// 数据索引
			tp.vindex++;
			tp.vindex &= 0x03;
			ADC1->JSQR = tp.yn_sel;
			IO_MODE_YM();
			tp.xv[tp.vindex] = adcv >>= 4; // to 10bits
			break;
		case 1:
			ADC1->JSQR = tp.z1_sel;
			IO_MODE_Z1M();
			tp.yv[tp.vindex] = adcv >>= 4; // to 10bits
			break;
		case 2:
			ADC1->JSQR = tp.z2_sel;
			IO_MODE_Z2M();
			tp.z1v[tp.vindex] = adcv >>= 4; // to 10bits
			break;
		case 3:
			ADC1->JSQR = tp.xn_sel;
			IO_MODE_XM();
			tp.z2v[tp.vindex] = adcv >>= 4; // to 10bits
			tp.mend = 1;
			break;
		default:
			break;
		}

		// 信息更新

		tp.step++;
		tp.step &= 0x03;
		tp.tout_cnt = 0;

		// 启动新转换
		ADC1->CR2 |= ADC_CR2_JSWSTART;
	}
	else
	{
		tp.tout_cnt++;
		if (tp.tout_cnt >= 3)
		{
			// ADC1->CR2 |= ADC_CR2_JSWSTART;
		}
	}
}
